package com.laoyu.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JavaDemo {
    public static void main(String[] args) {
        // 准备参数
        // 驱动
        String driver = "com.mysql.jdbc.Driver";
        // 链接地址
        String url ="jdbc:mysql://127.0.0.1:3306/jdbcdatabase?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
        // 数据库用户名
        String user = "root";
        // 数据库密码
        String password = "1234";
        // 开始链结
        // 声明连接对象
        Connection connection = null;
        // 加载驱动
        try {
            Class.forName(driver);
            // 获取连接
            connection= DriverManager.getConnection(url,user,password);
            // 打印
            if(connection!=null){
                System.out.println("连接成功...");
            }else {
                System.out.println("连接失败...");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
